package com.test.util;

import com.test.bean.FilterData;
import org.jdom.Element;

import java.util.List;

public class CreateElementUtil {

    public static String fileFolder = "D:\\newmapper\\xml";

    public static void createChildElement(Element selectElement, List<FilterData> allData) {
        int size = allData.size();
        for(int i = 0; i < size; i ++) {
            FilterData data = allData.get(i);
            Integer lastRow = data.getLastRow();
            Integer nextRow = data.getNextRow();
            String value = data.getValue();
            int space = data.getSpace();

            FilterData lastData = null;
            if (0 <= lastRow && lastRow < size) {
                lastData = allData.get(lastRow);
            }
            FilterData nextData = null;
            if (0 <= nextRow && nextRow < size) {
                nextData = allData.get(nextRow);
            }
            Boolean bool = checkParam(value);//是否传参，否表示传的实际值
            String paramName = value.replace("$", "");
            //获取sql前方的连接符
            StringBuilder text = new StringBuilder();

            //前面是否加入“连接符”和“（”
            StringBuilder beforeText = new StringBuilder();
            if (null == lastData) {
                beforeText.append(" and ");
                if(data.getSpace() > 0) {
                    beforeText.append(" ( ");
                }
              /*  if (null == lastData && (null != nextData && nextData.getSpace() < space)
                        || (null == lastData && null != nextData && nextData.getSpace() == space)) {
                    beforeText.append(" ( ");
                }*/
            }
            //前面是否是加入"上一行连接符"
            if (null != lastData) {
                beforeText.append(" " + lastData.getLink() + " ");
                if (lastData.getSpace() < space) {
                    beforeText.append(" ( ");
                } /*else if (lastData.getSpace() == space) {
                    beforeText.append(" " + lastData.getLink() + " ");
                }*/
            }

            //后面是否加“）”，加几个？
            StringBuilder afterText = new StringBuilder();
            //只要下面缩进不存在或者比当前行缩进小，就要考虑加多少“）”的问题; 还有一种情况，上面缩进全部一样并且都是缩进值>0，也要加 ）
            if(null == lastData && 0 < data.getSpace() &&  (null != nextData && nextData.getSpace() < data.getSpace())) {
                afterText.append(" ) ");
            }
            //获取需要添加的“）”的个数
            int number = 0;
            StringBuilder space_last = new StringBuilder(",");//查询上面的行中已经存在的缩进比本行小的值，用于去重
            if (null == nextData) {
                for(int k = i - 1; k >= 0; k --) {
                    FilterData data_0 = allData.get(k);
                    int dataSpace = data_0.getSpace();
                    if(null != data_0 && dataSpace < space) {
                        if(!space_last.toString().contains("," + dataSpace + ",")) {
                            number ++;
                            space_last.append(dataSpace).append(",");
                        }
                        if(dataSpace == 0) {//如果已经到了最小缩进，就不要继续遍历了
                            break;
                        }
                    }
                }
            }else if(nextData.getSpace() < space) {
                for(int k = i  - 1 ; k >= 0; k --) {
                    FilterData data_0 = allData.get(k);
                    int dataSpace = data_0.getSpace();
                    if(null != data_0 && dataSpace < space && dataSpace >= nextData.getSpace()) {
                        if(!space_last.toString().contains("," + dataSpace + ",")) {
                            number ++;
                            space_last.append(dataSpace).append(",");
                        }
                        if(dataSpace == 0) {//如果已经到了最小缩进，就不要继续遍历了
                            break;
                        }
                    }
                }
                if(!space_last.toString().contains(",0,")) {
                    number ++;
                }
            }
            for(int k = 0; k < number; k ++) {
                afterText.append(" ) ");
            }

            if (bool) {//考虑本行的参数值可能是$name 的形式，需要添加if判断，并且它会影响到其他行的数据
                selectElement.addContent(beforeText.toString());

                StringBuilder ifText = new StringBuilder();
                Element ifElement = new Element("if");
                ifElement.setAttribute("test", paramName + " != null");
                ifText.append(" " + data.getColumn() + getOperatorSql(data.getOperate(), "#{" + paramName + "}"));
                ifElement.setText(ifText.toString());
                selectElement.addContent(ifElement);
                //esle标签，直接将sql条件变为1 = 1
                Element elseElement = new Element("if");
                StringBuilder elseText = new StringBuilder();
                elseElement.setAttribute("test", paramName + " == null");
                elseText.append(" 1 = 1 ");
                elseElement.setText(elseText.toString());
                selectElement.addContent(elseElement);

                //2种情况： 没有下一行，并且上一行的缩进比本行低； 下一行和上一行的缩进都比本行低
                selectElement.addContent(afterText.toString());

            } else {//只考虑参数值都是实际值得情况
                text.append(beforeText.toString());
                text.append(" " + data.getColumn() + getOperatorSql(data.getOperate(), data.getValue()));
                text.append(afterText.toString());
                selectElement.addContent(text.toString());
            }
            //return selectElement;
        }
    }

    public static String getOperatorSql(String operator, String rightValue) {
        String value = "";
        switch (operator) {
            case "等于": value = " = '" + rightValue + "'"; break;
            case "不等于": value = " &lt;> '" + rightValue + "'"; break;
            case "小于": value = " &lt; '" + rightValue + "'"; break;
            case "小于或等于": value = " &lt;= '" + rightValue + "'"; break;
            case "大于": value = " > '" + rightValue + "'"; break;
            case "大于或等于": value = " >= '" + rightValue + "'"; break;
            case "包含": value = " like '%" + rightValue + "%'"; break;
            case "不包含": value = " not like '%" + rightValue + "%'"; break;
            case "开始以": value = " like '" + rightValue + "%'"; break;
            case "结束以": value = " like '%" + rightValue + "'"; break;
            case "是null": value = " is null"; break;
            case "不是null": value = " is not null" + rightValue; break;
            case "是空的": value = " = '' "; break;
            case "不是空的": value = " <> ''" + rightValue; break;
            case "介于": value = " between '" + rightValue.split(",")[0] + "' and '" + rightValue.split(",")[1] + "'"; break;
            case "非介于": value = " not between '" + rightValue.split(",")[0] + "' and '" + rightValue.split(",")[1] + "'"; break;
            case "在列表中": value = " in('" + rightValue.replace(",", "','") + ")"; break;
            case "不在列表中": value = " not in ('" + rightValue.replace(",", "','") + ")"; break;
            default: ;
        }
        return value;
    }

    //判断筛选条件中是否有传参
    public static Boolean checkParam(String value) {
        return (-1 != value.indexOf("$"));
    }
}
